/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.jsf;

import java.util.EnumMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import org.brainstretch.ejb.managers.QuizManager;
import org.brainstretch.models.CompletionStatus;
import org.brainstretch.models.UserQuiz;
import org.brainstretch.models.quizzes.Category;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author mrodaitis
 */
@ManagedBean
@RequestScoped
public class StatsController {

    @EJB
    QuizManager quizManager;
    
    @ManagedProperty("#{userController}")
    UserController userController;
    
    
    private PieChartModel chartModel;
    
    private EnumMap<CompletionStatus, Integer> resultMap;
    private CompletionStatus[] statuses;
    
    @PostConstruct
    public void init(){
        resultMap = new EnumMap<CompletionStatus, Integer>(CompletionStatus.class);
        statuses = CompletionStatus.values();
        for(CompletionStatus status: statuses){
            resultMap.put(status, 0);
        }
        chartModel = new PieChartModel();
        
        for(Category cat: Category.values()){
            List<UserQuiz> quizzes = quizManager.getQuizzesForUser(userController.getUser(), cat);
            int count = quizzes.size();
            for(UserQuiz quiz: quizzes){
                incrementResult(quiz.getStatus());
            }
            chartModel.set(cat.toString(), count);
        }
    }
    
    private void incrementResult(CompletionStatus status){
        resultMap.put(status, resultMap.get(status) + 1);
    }
    
    public int getStatusCount(CompletionStatus status){
        return resultMap.get(status);
    }

    public PieChartModel getChartModel() {
        return chartModel;
    }

    public void setChartModel(PieChartModel chartModel) {
        this.chartModel = chartModel;
    }

    public UserController getUserController() {
        return userController;
    }

    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    public CompletionStatus[] getStatuses() {
        return statuses;
    }
    
    
    
}
